/*******************************************************************************
 * Package: com.song.boot.springstudy.netty.simple.handler
 * Type:    NettyClient
 * Date:    2023-06-04 18:00
 *
 * Copyright (c) 2023 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.netty.chat.selfish;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.Scanner;


/**
 * 功能描述： 客户端
 *
 * @author Songxianyang
 * @date 2023-06-04 18:00
 */
public class ChartNettyClient {
    private Integer port;

    private String ip;

    public ChartNettyClient(Integer port, String ip) {
        this.port = port;
        this.ip = ip;
    }

    private void run() throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast("解码器", new StringDecoder());
                            pipeline.addLast("编码器", new StringEncoder());
                            pipeline.addLast(new ChartNettyClientHandler());
                        }
                    });
            System.out.println("客户端 is ok...");
            ChannelFuture channelFuture = bootstrap.connect(ip, port).sync();
            Channel channel = channelFuture.channel();
            InetSocketAddress socketAddress = (InetSocketAddress)channel.localAddress();
            System.out.println("获取客户端端口-----"+socketAddress.getPort()+"-------port--------");
            System.out.println("客户端地址------"+channel.remoteAddress()+"-----");
            System.out.println("用户id为：------"+socketAddress.getPort()+"-----");

            //客户端需要创建消息
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()){
                String msg = scanner.nextLine();
                //通过channel发送到服务器端
                channel.writeAndFlush(msg + "\n");
            }
            channel.closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ChartNettyClient(7000, "127.0.0.1").run();
    }
}
